﻿using Avalonia.Controls;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using MsBox.Avalonia.Enums;
using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 畜牧业审核程序国产.Models;
using 畜牧业审核程序国产.Models.ComparisonOutcome;
using 畜牧业审核程序国产.Models.ToolsClass;

namespace 畜牧业审核程序国产.ViewModels
{
    internal class ComparisonOutcomeViewModel:ObservableObject
    {
        private ObservableCollection<DataComparisonOutcome>  _dataComparisonOutcomes =new ObservableCollection<DataComparisonOutcome>();

        public ObservableCollection<DataComparisonOutcome> DataComparisonOutcomesCln
        {
            get { return _dataComparisonOutcomes; }
            set { SetProperty(ref _dataComparisonOutcomes, value); }
        }

        public RelayCommand DataComparisonOutCommand { get;private set; }
        public ComparisonOutcomeViewModel()
        {
            this.DataComparisonOutCommand = new RelayCommand(() => OutputDataComparison());
            WeakReferenceMessenger.Default.Register<TransmitView>(this, OnMyMessageReceived);
            WeakReferenceMessenger.Default.Register<List<DataComparisonOutcome>>(this, OnOutComeReceived);
      
        }

        private async void OutputDataComparison()
        {
            ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
            using (ExcelPackage package = new ExcelPackage())
            {
                ExcelWorksheet sht = package.Workbook.Worksheets.Add("一套表统计云对比结果");
                sht.Cells[1, 1].Value = "县";
                sht.Cells[1, 2].Value = "乡";
                sht.Cells[1, 3].Value = "户主姓名";
                sht.Cells[1, 4].Value = "指标名称";
                sht.Cells[1, 5].Value = "一套表数值";
                sht.Cells[1, 6].Value = "统计云数值";
                for (global::System.Int32 i = 0; i < DataComparisonOutcomesCln.Count; i++)
                {
                    sht.Cells[i + 2, 1].Value = DataComparisonOutcomesCln[i].Country;
                    sht.Cells[i + 2, 2].Value = DataComparisonOutcomesCln[i].Town;
                    sht.Cells[i + 2, 3].Value = DataComparisonOutcomesCln[i].FarmingName;
                    sht.Cells[i + 2, 4].Value = DataComparisonOutcomesCln[i].Indicator;
                    sht.Cells[i + 2, 5].Value = DataComparisonOutcomesCln[i].ValueInOldPlat;
                    sht.Cells[i + 2, 6].Value = DataComparisonOutcomesCln[i].ValueInStatsCloud;
                }
                // 自动调整列宽
                sht.Cells.AutoFitColumns();

                // 保存文件
                FileInfo file = new FileInfo(Path.Combine( MyDataFolderOperator.AuditOutcomeFolderPathInAuditProgramFolder, $"对比结果{DateTime.Now.ToString("yyyy年MM月dd日HH时mm分ss秒")}.xlsx"));
                package.SaveAs(file);
                await MyMessagebox.ShowAsync("导出完成,在审核结果文件夹", this.thisView, Icon.Success);
                thisView.Close();
            }
        }

        private void OnOutComeReceived(object recipient, List<DataComparisonOutcome> List_outcome)
        {
            for (int i = 0; i < List_outcome.Count; i++)
            {
                DataComparisonOutcomesCln.Add(new DataComparisonOutcome(List_outcome[i].Country, List_outcome[i].Town, List_outcome[i].FarmingName, List_outcome[i].Indicator, List_outcome[i].ValueInOldPlat, List_outcome[i].ValueInStatsCloud));
            }
        }

        private void OnMyMessageReceived(object recipient, TransmitView transmitView)
        {
            if (transmitView != null)
            {
                this.thisView = transmitView.TransmitedWindow;
            }
        }

        public Window thisView { get; set; }

        private void OnMyMessageReceived(object recipient, List< DataComparisonOutcome> List_outcome)
        {
            for (int i = 0; i < List_outcome.Count; i++)
            {
                DataComparisonOutcomesCln.Add(List_outcome[i]);
            }
        }
    }
}
